package androidx.plmgrdemo;

import android.app.Application;
import android.content.res.AssetManager;
import android.os.Environment;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import androidx.pluginmgr.PluginManager;

/**
 * @author Lody
 * @version 1.0
 */
public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        PluginManager.init(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                 copyAssetsFile("qrcode_debug.apk", sdcard);
            }
        }).start();
    }

    private static final String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
    private Boolean copyAssetsFile(String filename, String des) {
        Boolean isSuccess = true;
        //复制安卓apk的assets目录下任意路径的单个文件到des文件夹，注意是否对des有写权限
        AssetManager assetManager = this.getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            String newFileName = des + "/" + filename;
            out = new FileOutputStream(newFileName);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        return isSuccess;

    }

}
